package com.liuheng.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuheng.manager.mapper.SysRoleMapper;
import com.liuheng.manager.service.SysRoleService;
import com.liuheng.manager.service.SysRoleUserService;
import com.liuheng.spzx.common.log.annotation.Log;
import com.liuheng.spzx.model.dto.system.SysRoleDto;
import com.liuheng.spzx.model.entity.system.SysRole;
import com.liuheng.spzx.model.entity.system.SysRoleUser;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/admin/system/sysRole")
@RestController
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleUserService sysRoleUserService;
    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable Integer current, @PathVariable Integer limit
    , @RequestBody SysRoleDto sysRoleDto){
        Page<SysRole>  page = new Page(current,limit);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysRole::getId).like(sysRoleDto!=null,SysRole::getRoleName, sysRoleDto.getRoleName());
        Page<SysRole> page1 = sysRoleService.page(page, queryWrapper);
//        List<SysRole> records = page1.getRecords();
//        System.out.println(records);
        return Result.build(page1, ResultCodeEnum.SUCCESS);

}
    @Log(title = "角色添加",businessType = 0) //添加Log注解，设置属性
    @PostMapping("/saveSysRole")
    public Result save(@RequestBody SysRole sysRole ){
        boolean save = sysRoleService.save(sysRole);
        if (save){
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }else {
            return Result.build(null, ResultCodeEnum.DATA_ERROR);
        }
    }
    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        boolean update = sysRoleService.updateById(sysRole);
        if (update){
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }else {
            return Result.build(null, ResultCodeEnum.DATA_ERROR);
        }
    }
    @DeleteMapping("/deleteById/{roleId}")
    public Result deleteById(@PathVariable Long roleId){
    sysRoleService.removeById(roleId);
    return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping(value = "/findAllRoles/{userId}")
    public Result<Map<String, Objects>>findAllRoles(@PathVariable("userId")Long userId){
        LambdaQueryWrapper<SysRoleUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId!=null,SysRoleUser::getUserId,userId);
        List<SysRole> sysRoleList=null;
            sysRoleList = sysRoleService.list();
        List<SysRoleUser> sysUserRoles = sysRoleUserService.list(queryWrapper);
        List<Long> sysRoles=new ArrayList<>();
        for (int i = 0; i < sysUserRoles.size(); i++) {
            Long roleId = sysUserRoles.get(i).getRoleId();
            sysRoles.add(roleId);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList",sysRoleList);
        map.put("sysUserRoles",sysRoles);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }
}
